<?php
namespace app\admin\controller;
use think\Controller;
use \traits\controller\Jump;
use think\Db;
class Login extends Controller{
	public function login(){
		return $this->fetch();
	}
	/**
	 * 登录
	 */
	public function checkLogin(){
		$code=input('verify');
		$data['account']=input('account');
		$data['password']=md5(input('password'));
		if(!captcha_check($code))
		{
			$this->success('验证码错误！','login');
		} else {
			$admin = db('admin')->where($data)->find();
			if($admin){
				if($admin['useflag']=='0'){
					$this->error('用户未激活，请联系管理员！');
				}
				session('name',$admin['name']);
				session('is_login',1);
				session('account',$admin['account']);
				session('admin_id',$admin['id']);
				$this->success('登录成功！', 'Index/index');
			}else{
				$this->error('用户名或密码错误！');
			}
			
		}
	}
	/**
	 * 修改资料
	 */
	public function update(){
		if($this->request->isPost()){
			$requestArr=$this->request->param();
			if($requestArr['password']){
				$requestArr['password'] = md5($requestArr['password']);
				db('admin')->update($requestArr);
			}else{
				$data['id'] = $requestArr['id'];
				$data['account'] = $requestArr['account'];
				$data['name'] = $requestArr['name'];
				db('admin')->save($data);
			}
			session(null);
			$this->success('修改成功','Login/login');
			exit;
		}
		$id = session('admin_id');
		$obj = db('admin')->where("id = $id")->find();
		$this->assign('obj',$obj);
		return $this->fetch();
	}
	/**
	 * 登出
	 */
	public function loginout(){
		session(null);//清楚session
		header("Location:".url('Login/login'));exit;
	}
}